function raicar_generateIndex (totalComp, indexNm, compPerPage)

if ~isa (totalComp, 'numeric') || mod (totalComp, 1) ~= 0
    error ('generateIndex: the first input, totalComp, is not integer');
end
if exist (strcat (indexNm, '1.html'), 'file')
    error ('generateIndex: the target file exist!');
end

[~, filenm] = fileparts (indexNm);

numPages = ceil (totalComp/compPerPage);

for page = 1:numPages

% generate index page
fid = fopen (strcat (indexNm, num2str(page), '.html'), 'w');
fprintf (fid, '<html>\n');
fprintf (fid, '<head>\n');
fprintf (fid, '\t<title>RAICAR Report: Index (page %d of %d)</title>\n', page, numPages);
fprintf (fid, '</head>\n');
fprintf (fid, '<body>\n');
fprintf (fid, '\t<h1 align="center">RAICAR Report (page %d of %d)</h1>\n', page, numPages);
% make page links if there are more than one page
if numPages ~= 1
    fprintf (fid, '\t<h2 align="center">');
    for p = 1:numPages
        fprintf (fid, '<a href="%s">%d</a>  ', strcat (filenm, num2str(p), '.html'), p);
    end
    fprintf (fid, '</h2>\n');
end

fprintf (fid, '<hr>\n<br><br>\n'); 

for compInd = 1:min (compPerPage, totalComp-compPerPage*(page-1))
comp = (page-1)*compPerPage+compInd;
% table and caption
fprintf (fid, '\t<br><br>\n');
fprintf (fid, '\t<table border="0" cellpadding="0">\n');
fprintf (fid, '\t<caption><h2>Component %d</h2></caption>\n', comp);

% leave some space
fprintf (fid, '\t\t<tr>\n');
fprintf (fid, '\t\t\t<td>&nbsp;</td>\n');
fprintf (fid, '\t\t</tr>\n');

% show IC map
fn = sprintf ('map_IC%d.png', comp);
fn2 = sprintf ('map_IC_nothresh%d.png', comp);
fprintf (fid, '\t\t<tr>\n');
fprintf (fid, '\t\t\t<td align="left" colspan="3"><a href="%s"><img src="%s" width="834" height="500" border="0"></a></td></td>\n', fn2, fn);
fprintf (fid, '\t\t</tr>\n');

% leave some space
fprintf (fid, '\t\t<tr>\n');
fprintf (fid, '\t\t\t<td>&nbsp;</td>\n');
fprintf (fid, '\t\t</tr>\n');

% header for the 2nd row
fprintf (fid, '\t\t<tr>\n');
fprintf (fid, '\t\t\t<th>Reproducibility rank</th>\n');
fprintf (fid, '\t\t\t<th>Mixing time course and power spectrum</th>\n');
fprintf (fid, '\t\t</tr>\n');
    
% show 2 figures in the 2nd row
fprintf (fid, '\t\t<tr>\n');
fn = sprintf ('rank_IC%d.png', comp);
fprintf (fid, '\t\t\t<td><img src="%s" align="left" width="333" height="333" border="0"></td>\n', fn);
fn = sprintf ('tc_IC%d.png', comp);
fprintf (fid, '\t\t\t<td><img src="%s" align="left" width="500" height="333" border="0"></td>\n', fn);
fprintf (fid, '\t\t</tr>\n');

fprintf (fid, '\t</table>\n');
fprintf (fid, '\t<br><br>\n');
fprintf (fid, '\t<hr>\n');
end

% links for mulitple pages again
if numPages ~= 1
    fprintf (fid, '\t</br></br>\n');
    fprintf (fid, '\t<h2 align="center">');
    for p = 1:numPages
        fprintf (fid, '<a href="%s">%d</a>  ', strcat (filenm, num2str(p), '.html'), p);
    end
    fprintf (fid, '</h2>\n');
end
    
fprintf (fid, '\t<h3 align="center">Automatically Generated by gRAICAR</h3>\n');
fprintf (fid, '</body>\n');
fprintf (fid, '</html>\n');
fclose (fid);
end

